﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="sRoleform.aspx.cs" Inherits="SIPO.Web.Forms.UserManagement.sRoleform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Role Form</title>

    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
 
    </script>

    <script type="text/javascript">

        $(document).ready(function () {

            // 1. prepare the validation rules and messages.
            var rules = {
                txtDesc: {
                    required: true,
                    maxlength: 50
                }
            };
            var messages = {
                txtDesc: {
                    required: "Nama Role wajib di isi.",
                    maxlength: "Panjang maksimal nama role adalah 50"
                }
            };

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            // 3. Set the click event to do the validation
            $("#btnSave").click(function () {

                var role_name = $('#txtDesc').val();
                var id = $('#hfID').val();
                var status = $('#hfStatus').val();
                
                var name = $('#txtDesc').val();
                var isInternal = 0;
                if (status == -1)
                    isInternal = 1;
                else
                    isInternal = 0;

                if (!validator.validate())
                    return;

                var handlerUrl1 = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiNamaRole.ashx?name=' + name + '&isInternal=' + isInternal + '") %>';
                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sRolehandler.ashx") %>';

                var objRole = new Object();
                objRole.RoleID = id;
                objRole.RoleName = encodeURIComponent(role_name);
                objRole.Status = encodeURIComponent(status);

                jsonData = JSON.stringify(objRole);

                if (name != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl1,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.Name == 0) {
                                if (id == 0)
                                    CallAjax(handlerUrl, "?method=insert", jsonData);
                                else {
                                    CallAjax(handlerUrl, "?method=update", jsonData);
                                }

                                parent.$('#dialog').dialog("close");

                                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                                return false;
                            } else {
                                alert('data nama role ada yang sama!');
                                $('#txtDesc').val('');
                                $('#txtDesc').focus();
                            }
                        },

                        error: function () {
                            alert('Error!');
                        }
                    });
                }

            });

        });
    
    </script>
    <script type="text/javascript">

        function ClearText() {
            $('#txtDesc').val('');
        }

        $('#btnClose').click(function () {
            parent.$('#dialog').dialog("close");
        });
         
    </script>
</head>
<body onload="ClearText();">
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Role
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtDesc" runat="server" Width="300" MaxLength="100"></asp:TextBox>&nbsp;<span style="color:Red;">*</span>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                        <button id="btnSave" type="button">Save</button>
                        <button id="btnClose" type="button">Close</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfStatus" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
